package org.apache.mina.example.lily;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

public class NIOConnection {

	private Selector selector;

	private ServerSocketChannel ssc;

	public void init(String host, int port) {
		try {
		    selector = Selector.open();
			ssc = ServerSocketChannel.open();
			ssc.configureBlocking(false);
			ssc.bind(new InetSocketAddress("localhost", 8080));
			ssc.register(selector, SelectionKey.OP_ACCEPT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void start(){
		for(;;){
			try {
				int selectcounts =selector.select();
				Iterator<SelectionKey> i =selector.selectedKeys().iterator();
				while(i.hasNext()){
					SelectionKey sk = i.next();
					i.remove();
					if(sk.isAcceptable()){
						
					}else if(sk.isConnectable()){
						
					}else if(sk.isReadable()){
						
					}else if(sk.isWritable()){
						
					}
					handle( sk );
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	private void handle(SelectionKey sk){
	}

	public static void main(String[] args) {
	}
}
